package com.gaojinqi.sys.core.shiro.token;

import com.gaojinqi.base.common.exception.BizRuntimeException;
import com.gaojinqi.sys.core.shiro.util.JwtTokenHelper;
import lombok.Getter;
import org.apache.shiro.authc.AuthenticationToken;

/**
 * Jwt Token
 *
 * @author gaojinqi
 * @version 1.0
 * @since 2020年05月09日
 */

@Getter
public class JwtToken implements AuthenticationToken {

    /**
     * 主题，一般是用户名
     */
    private String subject;

    /**
     * jwt
     */
    private String jwt;

    /**
     * 用户的IP<Br/>
     * The location from where the login attempt occurs, or <code>null</code> if not known or explicitly
     * omitted.
     */
    private String host;

    private JwtTokenHelper jwtTokenHelper;

    public JwtToken(String jwt, String host, JwtTokenHelper jwtTokenHelper) {
        if (jwtTokenHelper == null) {
            throw new BizRuntimeException("jwt配置错误");
        }
        this.jwt = jwt;
        this.host = host;
        this.subject = jwtTokenHelper.getSubject(jwtTokenHelper.getClaims(jwt));
    }

    public JwtToken(String jwt, JwtTokenHelper jwtTokenHelper) {
        this(jwt, null, jwtTokenHelper);
    }

    @Override
    public Object getPrincipal() {
        return this.subject;
    }

    @Override
    public Object getCredentials() {
        return this.jwt;
    }

}
